Page({
  data: {
    passport: {}
  },
  onLoad ({ id, encryptedData }) {
    // console.log(id)
    // console.log(encryptedData)
    this.getPassport(id)
    this.getSharePassport(encryptedData)
  },
  // 获取访客详情（通行证）
  async getPassport (id) {
    const data = await wx.http.get('/visitor/' + id)
    // console.log(data)
    this.setData({ passport: data.data })
  },
  async getSharePassport (encryptedData) {
    // 检测是否存在 id
    if (!encryptedData) return
    // 调用接口
    const { data: passport } = await wx.http.get('/visitor/share/' + encryptedData)
    // 渲染数据
    this.setData({ passport })
  },

  onShareAppMessage () {
    // 获取加密数据
    const { encryptedData } = this.data.passport
    // console.log(encryptedData)
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png'
    }
  },
  //保存图片
  async saveQRCode () {
    try {
      const { path } = await wx.getImageInfo({
        // 二维码的图片路径
        src: this.data.passport.url
      })
      // 保存图片到相册
      wx.saveImageToPhotosAlbum({ filePath: path })
    } catch (error) {
      console.log(error)
    }
  }
})
